#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QTcpServer>
#include <QTcpSocket>
#include <QSettings>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QSqlError>

#include "../MyTcp2/pack.h"
#include "../MyTcp2/tcpsession.h"
#include "tbl_user_dao.h"
#include "tbl_house_dao.h"

QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE

class MyHouse;

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = nullptr);
    ~Widget();

public slots:
    void onNewConnection();
    void onReadyRead();
    void onDisconnected();

private:
    //业务报文处理函数
    bool handle(TcpSession* session, Pack& rcvPack, Pack& retPack);
    //各类报文拆函数处理
    bool handleLogin(TcpSession* session,Pack& rcvPack, Pack& sendPack);
    bool handleReg(Pack& rcvPack, Pack& sendPack);
    bool handleLogout(Pack& rcvPack, Pack& sendPack);
    bool handleHouseAdd(Pack& rcvPack, Pack& sendPack);
    bool handleHouseList(Pack& sendPack);
    bool handleHouseDel(Pack& rcvPack, Pack& sendPack);
    bool handleHouseUpdate(Pack& rcvPack, Pack& sendPack);
    bool handleHouseEnter(TcpSession* session, Pack& rcvPack, Pack& sendPack);
    bool handleHouseExit(TcpSession* session, Pack& rcvPack);
    bool handleHouseBullet(TcpSession* session, Pack& rcvPack);
    bool handleHouseGift(TcpSession* session, Pack& rcvPack);
    bool handleHouseVideo(TcpSession* session, Pack& rcvPack);
    bool handleHouseCamera(TcpSession* session, Pack& rcvPack);

private:
    Ui::Widget *ui;
    QTcpServer* server;
    QTcpSocket* socket;
    QSettings* settings;
    //以下要删除与整合
    QSqlDatabase db;
    tbl_user_dao* user_dao;
    tbl_house_dao* house_dao;

    //直播间 key-房间的seiral， value-房间里的成员列表
    QMap<int, QMap<QString, TcpSession*>> houseMap;

    //用户登录列表
    QMap<QString, TcpSession*> userMap;

};
#endif // WIDGET_H
